package Q387

/**
 * 字符串中第一个唯一字符
 * @author 23737
 * @date 2022.5.9
 */
fun main() {
    val s = "leetcode"
    val firstUniqChar = Solution().firstUniqChar(s)
    println(firstUniqChar)
}

class Solution {
    fun firstUniqChar(s: String): Int {
        val map: MutableMap<Char, Int> = HashMap<Char, Int>()
        if (s.isEmpty()) return -1
        var toCharArray = s.toCharArray()

        for (i in toCharArray) {
            map[i] = map.getOrDefault(i,0)+1
        }

        for(i in s.indices){
            if(map[toCharArray[i]] == 1){
                return i
            }
        }

        return -1
    }


}